package Year22_Month06.day06;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class N叉树的层序遍历_BFS {
    public List<List<Integer>> levelOrder(Node root) {
        Queue<Node> queue = new LinkedList<>();//用来遍历每一层的节点
        List<List<Integer>> list = new ArrayList<>();
        if(root != null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
            int size = queue.size();
            List<Integer> mylist = new ArrayList<>();
            while(size-- > 0){
                Node node = queue.poll();
                mylist.add(node.val);
                //把当前节点的所有子节点加入当前层次的mylist当中
                for(Node chileNode:node.children){
                    queue.offer(chileNode);
                }
            }
            list.add(mylist);
        }
        return list;
    }
}
